//
//  IconScrollerView.m
//  IconMaster
//
//  Created by mac on 2017/9/11.
//  Copyright © 2017年 yangweiwei. All rights reserved.
//

#import "IconScrollerView.h"

#define SELFWIDE self.bounds.size.width
#define SELFHeIGHT self.bounds.size.height
#define Space 10


@interface IconScrollerView ()

@property (nonatomic, strong) UIButton *iconButton;
@property (nonatomic, strong) UIButton *moreButton;

@end

@implementation IconScrollerView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)initSubViewsInArr{
//    self.userInteractionEnabled  =YES;
    UIScrollView *backSCV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SELFWIDE, SELFHeIGHT)];
    backSCV.contentSize = CGSizeMake(SELFHeIGHT *self.contentArr.count + Space *self.contentArr.count +SELFHeIGHT, self.bounds.size.height);
    backSCV.scrollEnabled = YES;
    backSCV.showsHorizontalScrollIndicator = false;
    [self addSubview:backSCV];
    
    for (int i = 0; i < self.contentArr.count; i++) {
        _iconButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _iconButton.frame = CGRectMake(SELFHeIGHT * i + Space * i, 0, SELFHeIGHT, SELFHeIGHT);
         _iconButton.tag = i;
//        if ([NSURL URLWithString:self.contentArr[i]]) {
//             [self.iconButton.imageView sd_setImageWithURL:[NSURL URLWithString:self.contentArr[i]]];
//        }else{
//        iconButton.imageView.frame = iconButton.bounds;
//        iconButton.hidden = NO;
//        iconButton.imageView.image = [UIImage imageNamed:self.contentArr[i][@"path"]];
//        iconButton.backgroundColor = [UIColor redColor];;
            [_iconButton setImage:[UIImage imageNamed:[self deleteString:self.contentArr[i][@"path"]] ]  forState:Normal];
//        iconButton.enabled = NO;
//        }
//        [iconButton setBackgroundImage:[UIImage imageNamed:self.contentArr[i][@"path"]] forState:Normal];
        [_iconButton addTarget:self action:@selector(buttonAction:) forControlEvents:TouchUPInside];
        [backSCV addSubview: _iconButton];
    }
    _moreButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.iconButton.frame) + Space, 0, SELFHeIGHT, SELFHeIGHT)];
//    [_moreButton setTitle:@"更多" forState:Normal];
    [_moreButton setImage:[UIImage imageNamed:@"BGoMoreBtn"] forState:Normal];
    [_moreButton addTarget:self action:@selector(buttonAction:) forControlEvents:TouchUPInside];
    _moreButton.tag = self.contentArr.count;
    [backSCV addSubview:_moreButton];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1.5, _moreButton.height)];
    lineView.backgroundColor = [UIColor whiteColor];
    [_moreButton addSubview:lineView];
}
- (void)buttonAction:(UIButton *)button{

    if (button.tag == self.contentArr.count) {
        if ([self.delegate respondsToSelector:@selector(butonMore)]) {
            [self.delegate butonMore];
        }
    }else{
    
        if ([self.delegate respondsToSelector:@selector(buttonImage:)]) {
            [self.delegate buttonImage:button.imageView.image];
        }
    }
    NSLog(@"%ld", (long)button.tag);
}
- (NSString *)deleteString:(NSString *)string{

    return  [string stringByReplacingOccurrencesOfString:@":" withString:@""];
}
@end
